﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace VK.Casemeet.Groups
{
    public class GroupMembersDAL
    {
        /// <summary>
        /// 添加报名信息.
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        public static int AddGroupsUser(GroupMembers group)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder InsertGroupUser = new StringBuilder();

            InsertGroupUser.Append("insert into GroupUser (groupid,memberid)values(@groupid,@memberid) update Groups set g_SigupTotal=g_SigupTotal+1 where groupID=@groupid");
            DbCommand InsertMemberCommand = db.GetSqlStringCommand(InsertGroupUser.ToString());

            db.AddInParameter(InsertMemberCommand, "@groupid", DbType.Int32, group.groupid);
            db.AddInParameter(InsertMemberCommand, "@memberid", DbType.Int32, group.memberid);
            return db.ExecuteNonQuery(InsertMemberCommand);
        }

        /// <summary>
        /// 是否参加过团购
        /// </summary>
        /// <returns></returns>
        public static bool IsSignupGroup(int groupid,int userid)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder InsertGroupUser = new StringBuilder();

            InsertGroupUser.Append("select count(*) from GroupUser where groupid=@groupid and memberid=@memberid");
            DbCommand InsertMemberCommand = db.GetSqlStringCommand(InsertGroupUser.ToString());

            db.AddInParameter(InsertMemberCommand, "@groupid", DbType.Int32, groupid);
            db.AddInParameter(InsertMemberCommand, "@memberid", DbType.Int32, userid);
            if (Convert.ToInt32(db.ExecuteScalar(InsertMemberCommand)) == 0)
                return false;
            return true;
        }
    }
}
